home *** CD-ROM | disk | FTP | other *** search
/ ftp.cse.unsw.edu.au / 2014.06.ftp.cse.unsw.edu.au.tar / ftp.cse.unsw.edu.au / pub / doc / misc / Dan Bernstein's Job Control Scheme < prev    next >
Encoding:
Internet Message Format  |  1992-10-18  |  24.9 KB

  1. Path: usage.csd.unsw.oz.au!metro!munnari.oz.au!spool.mu.edu!think.com!snorkelwacker.mit.edu!hsdndev!cmcl2!kramden.acf.nyu.edu!brnstnd
  2. From: brnstnd@kramden.acf.nyu.edu (Dan Bernstein)
  3. Newsgroups: comp.unix.wizards
  4. Subject: How to add job control to a UNIX system [long]
  5. Message-ID: <11513.Aug204.03.1191@kramden.acf.nyu.edu>
  6. Date: 2 Aug 91 04:03:11 GMT
  7. Organization: IR
  8. Lines: 501
  9.  
  10. A number of people have asked me to post this, so here it is. A word of
  11. caution to those who have indicated interest in implementing job control
  12. on MINIX, various System V releases, et al.: This paper has not been
  13. extensively reviewed. Please watch unix-wizards during August for any
  14. corrections, clarifications, or further drafts.
  15.  
  16. ---Dan
  17.  
  18. How to add job control to a UNIX system
  19. Daniel J. Bernstein
  20. draft 2
  21. 8/1/91
  22.  
  23.  
  24. Abstract
  25.  
  26. We describe in detail the steps necessary to add BSD-style job control
  27. to any UNIX system. In place of the BSD and POSIX rules for controlling
  28. ttys, sessions, and process groups, we propose a very simple yet secure
  29. mechanism for manipulating process groups alone. This mechanism can also
  30. be added to existing BSD systems to provide an alternate, easier-to-use
  31. programming interface.
  32.  
  33.  
  34. 1. Introduction
  35.  
  36. Sections 2 through 6 describe selected portions of BSD 4.2 and 4.3 job
  37. control. Omitted is any mention of controlling ttys, POSIX sessions,
  38. getpgrp() and setpgrp(), TIOCGPGRP and TIOCSPGRP, tcgetpgrp() and
  39. tcsetpgrp(), TIOCNOTTY, setsid(), TIOCSCTTY, setpgid(), open() with or
  40. without O_NOCTTY, and the relations between all of those and the rest of
  41. the job control system, because it turns out that none of that is
  42. necessary to provide job control. The attitude in these sections is that
  43. of someone faced with a System V variant or a new UNIX system (e.g.,
  44. MINIX) with no job control facilities in the first place, perhaps
  45. without even the concept of a controlling tty; the important question is
  46. how little work is necessary to add job control features.
  47.  
  48. Section 7 describes my new, secure, extremely simple job control
  49. programming interface [1]. (The interface was inspired by a comment from
  50. Chris Torek. It was modified slightly in response to criticism by
  51. John Carr. It is dedicated to Marc Teitelbaum.) The interface is enough
  52. to let programmers implement a job control shell or any other job
  53. control-cognizant applications. It solves all the problems that POSIX
  54. sessions were meant to solve, but it is much, much simpler, and can be
  55. added to a system with a minimum of effort. It can even be added to a
  56. BSD system, as discussed in section 8---it does not interfere with the
  57. old job control model in any way. This will give programmers a choice
  58. between the older, more complicated interface and this new, easy-to-use
  59. interface.
  60.  
  61. Section 9 lists several job control programming techniques. Finally,
  62. section 10, again from the point of view of a system without job
  63. control, lists some common macros and similar cpp-level extensions
  64. which make job control programs easier to port.
  65.  
  66.  
  67. 2. New kernel structures
  68.  
  69. Each process ``is a member of'' a process group. In other words, there's
  70. a p_pgrp integer inside struct proc. init starts out in process group 0.
  71. Process groups remain the same across fork() and exec().
  72.  
  73. Each tty has a ``foreground'' process group. In other words, there's a
  74. t_pgrp integer inside struct tty. (Systems where ttys are implemented
  75. differently, e.g., via streams, will have to store this information
  76. somewhere else.) A tty opened for the first time has t_pgrp set to 0.
  77. Each tty also has one extra keyboard character, the suspend character,
  78. with a default of 26 (^Z).
  79.  
  80. There is a new process state (i.e., p_stat value): SSTOP. (ps usually
  81. reports this state as T.) When a process is in this state, it gets no
  82. CPU time. All signals are blocked until it leaves the state. Note that
  83. systems with some form of process tracing (e.g., ptrace(2)) already have
  84. SSTOP.
  85.  
  86.  
  87. 3. Signals
  88.  
  89. There are five new signals declared in <signal.h>: SIGSTOP (17), SIGTSTP
  90. (18), SIGCONT (19), SIGTTIN (21), SIGTTOU (22). (The numbers in
  91. parentheses are the standard BSD values.) Any code which works with bit
  92. masks representing signals must be prepared to work with 32-bit masks.
  93.  
  94. The default action of a process receiving SIGSTOP, SIGTSTP, SIGTTIN, or
  95. SIGTTOU is to stop, i.e., enter the SSTOP state and, as detailed below,
  96. to generate a SIGCHLD. SIGSTOP cannot be blocked, caught, or ignored.
  97. (``Blocking'' refers to any mechanism by which the receipt of a signal
  98. is deferred. BSD provides sigblock() and sigsetmask() to manipulate a
  99. bit mask of blocked signals. On systems without a similar mechanism,
  100. SIGSTOP obviously can't be blocked in the first place. What's important
  101. is tht SIGSTOP always take effect immediately.)
  102.  
  103. Any process which receives SIGCONT will continue, i.e., leave the SSTOP
  104. state; this is in addition to any signal handler installed. (Obviously
  105. the process cannot execute a signal handler if it's in the SSTOP state,
  106. receiving no CPU time!) SIGCONT cannot be blocked. A process is always
  107. able to send SIGCONT to any of its children, regardless of permission
  108. checks. (BSD actually lets you send SIGCONT to any descendant. Some
  109. popular BSD variants do not obey this rule.)
  110.  
  111. When a process enters the SSTOP state, it generates a SIGCHLD (aka
  112. SIGCLD) to its parent. There are several conflicting sets of semantics
  113. for SIGCHLD/SIGCLD (e.g., what happens when it's ignored? when are
  114. zombies created?) on various systems, none of which have any relevance
  115. to job control.
  116.  
  117.  
  118. 4. Waiting
  119.  
  120. When the parent, either upon receiving a SIGCHLD or at any other time,
  121. does a wait(), it will not see any stopped children---i.e., job control
  122. doesn't change the semantics of wait(). (Process tracing does, but that
  123. is also irrelevant to job control.) There is a new system call, wait2,
  124. which lets the parent see stopped children:
  125.  
  126.   #include <sys/wait.h>
  127.  
  128.   int wait2(status,options)
  129.   int *status;
  130.   int options;
  131.  
  132. (In fact, BSD has a wait3() call instead of wait2(); the above call is
  133. the same as wait3(status,options,(struct rusage *) 0). See section 10
  134. for further details.) options is a bit field. You have to define two
  135. bits, WNOHANG and WUNTRACED, in <sys/wait.h>, for use as options.
  136.  
  137. Normally wait2() acts like wait(): it blocks waiting for a child to die
  138. and then returns the dead pid, or returns -1 immediately if there are no
  139. live children. If options includes WNOHANG, wait2() will return 0
  140. immediately instead of blocking. If options includes WUNTRACED, wait2()
  141. will return the pid of a stopped child as well as the pid of a dead
  142. child. (By far the most common options value is WNOHANG | WUNTRACED.)
  143.  
  144. As usual, when wait2() returns a pid, status says what's happened to
  145. that pid. This is a bit more complicated than before because status also
  146. has to tell the parent what happened if the child stopped. Here's the
  147. whole story: If the low 7 bits are all set, the child has in fact
  148. stopped. If none of those bits are set, the child has exited normally.
  149. Otherwise the child has been terminated by a signal, and those 7 bits
  150. say which signal it was. (If the 8th bit is set in that case, the child
  151. dumped core.) If the child has stopped, the 8th bit is 0, and the 8 bits
  152. after that say which signal (SIGTTOU, for instance) stopped the process.
  153. If the child has exited, the 8th bit is 0, and the 8 bits after that
  154. give its exit code mod 256.
  155.  
  156.  
  157. 5. Terminal-generated signals
  158.  
  159. When the interrupt character (typically ^C) is typed on a terminal in
  160. cooked mode, if the terminal's foreground process group is non-zero,
  161. every process in that process group is sent SIGINT. Similarly, the quit
  162. character (typically ^\ under BSD, DEL under System V) generates
  163. SIGQUIT. If a terminal is ``hung up'', it generates SIGHUP. Job control
  164. needs one extra signal so that the user can tell the current process to
  165. stop: namely, the suspend character mentioned above (typically ^Z),
  166. which generates SIGTSTP. Notice that if a user could set his tty's
  167. process group arbitrarily, he could send all sorts of signals to any
  168. processes in those process groups. So it is important for security that
  169. tty process groups be controlled.
  170.  
  171. The suspend character is the first user interface aspect of job control
  172. mentioned so far. Typically the processes stop (though they can catch
  173. SIGTSTP and do something else). A job-control shell then receives the
  174. SIGCHLD and, with wait3(), sees that its children have stopped. It can
  175. report this to the user and present a new prompt. The user can then
  176. start more processes, or, with an ``fg'' (foreground) command, tell the
  177. shell to send SIGCONT to the children so that they start up again.
  178.  
  179. Programs can inspect and set the suspend character with two new tty
  180. ioctls: TIOCGLTC and TIOCSLTC, both defined in <sys/ioctl.h>. In both
  181. cases the argument points to a ``struct ltchars'' (defined in the same
  182. place), which contains a char t_suspc specifying the suspend character.
  183.  
  184. As a matter of fact, under BSD there are several other local terminal
  185. characters (that's what ltchars stands for), notably t_dsuspc. The
  186. delayed suspend character (typically ^Y) is supposed to act like the
  187. suspend character but only when a process actually reads it. However,
  188. several operating system releases from Sun simply don't do this. They
  189. pass dsusp through like any other character. Given that almost nobody
  190. ever notices this bug, let alone complains about it, I don't think
  191. there's any point in bothering to implement the character.
  192.  
  193.  
  194. 6. I/O-generated signals
  195.  
  196. There's another side to the job control user interface: namely, several
  197. processes (or pipelines---in general, ``jobs'') can read and write the
  198. tty at once. The job-control shell places each pipeline into a separate
  199. process group, and when any job except the foreground job reads from the
  200. tty, it is stopped until the user decides to give it input. This is much
  201. more flexible than cutting background processes off from the tty
  202. permanently, as non-job-control shells do.
  203.  
  204. More precisely, if a process reads from a tty, and its process group is
  205. not the foreground process group of the tty, then its process group is
  206. sent a SIGTTIN signal. As an exception, if that process is blocking or
  207. ignoring SIGTTIN, no signal is generated. Instead, the read returns -1
  208. with errno of EIO. ``Reading'' here includes only read(), not the
  209. various tty ioctls which inspect tty structures; while there are some
  210. benefits of generating SIGTTIN for the latter, this turns out to be too
  211. restrictive for many applications. (There is an ioctl, TIOCSTI, which is
  212. also lumped with ``reading,'' but a full discussion of TIOCSTI would be
  213. too long for this paper. It's not an important enough ioctl to bother
  214. with.)
  215.  
  216. If a process writes to a tty, and its process group is not the
  217. foreground process group of the tty, then its process group is sent a
  218. SIGTTOU signal. As an exception, if that process is blocking or ignoring
  219. SIGTTOU, no signal is generated and it is allowed to produce output.
  220. This time, ``write'' includes not only write() but also any other
  221. operations which affect the tty in any way. (Under BSD there is a tty
  222. mode, LTOSTOP, which when disabled turns off TTOU for write() but not
  223. for other operations. This is not absolutely necessary, but if you have
  224. any free time you should implement stty tostop to turn LTOSTOP on and
  225. stty -tostop to turn it off. The internal interface is unimportant as
  226. long as the user can select his favorite behavior.)
  227.  
  228. None of the above apply to operations by a process in process group 0.
  229. Process group 0 must never, ever, be sent I/O-generated signals. The
  230. simplest course of action here is to let all operations from process
  231. group 0 succeed. (What actually happens in this case isn't too
  232. important, as long as processes like getty can open a tty and start
  233. programs on the tty. Most BSD-derived systems set process group to pid
  234. when a process in process group 0 opens a tty; this behavior is not
  235. necessary. Note that if a process in process group 0 reads from a tty
  236. while a shell is still reading from it, the two read()s will compete for
  237. terminal input.)
  238.  
  239. Notice that if a process can join an arbitrary process group, it can
  240. cause SIGTTOU and SIGTTIN to be sent to other process. So it's important
  241. for security that processes' process groups be controlled.
  242.  
  243. Be careful in implementing I/O-generated signals that you test
  244. repeatedly for the right process group. The process could easily receive
  245. SIGCONT while the tty is in a different group. In that case it should
  246. immediately stop the process group again (without even executing a
  247. SIGCONT handler!), generate another SIGCHLD, and wait for the next
  248. SIGCONT. This can repeat any number of times. Only when the tty is in
  249. the right process group should the operation succeed.
  250.  
  251.  
  252. 7. A new, secure, simple job control programming interface
  253.  
  254. The process group calls described in this section are, unlike the job
  255. control features described in sections 2 through 6, not part of BSD,
  256. though they do not interfere with BSD. There are a total of three calls
  257. which manipulate process groups: tcnewpgrp(), settpgrp(), tctpgrp().
  258. Throughout this section, fdtty is a file descriptor pointing to a
  259. terminal.
  260.  
  261. If fdtty has write access, tcnewpgrp(fdtty) should allocate an unused
  262. process group and set the terminal's foreground process group to that
  263. new process group. This is a write operation and should produce SIGTTOU
  264. if this process is not in the foreground (and is not ignoring the
  265. signal, etc.). tcnewpgrp returns 0 on success, -1 with errno ENOTTY if
  266. fdtty is not a terminal, -1 with errno EBADF if fdtty is not open for
  267. writing.
  268.  
  269. If fdtty has read access, settpgrp(fdtty) should set this process's
  270. process group to the foreground process group of the terminal. As a
  271. special case, settpgrp(-1) sets this process's process group to 0, so
  272. that it is exempt from job control. The latter is redundant---a process
  273. can just as easily create a process group for itself, fork, and hide the
  274. child away inside that group---but convenient. settpgrp returns 0 on
  275. success, -1 with errno ENOTTY if fdtty is not -1 and not a terminal, -1
  276. with errno EBADF if fdtty is not open for reading.
  277.  
  278. If fdtty has write access, and pid is the current process or a child of
  279. the current process, tctpgrp(fdtty,pid) should set the terminal's
  280. foreground process group to the process group of pid. This is a write
  281. operation. You may want to allow pid to be any descendant of the current
  282. process (under BSD this simplifies the implementation), but this is not
  283. necessary for a job control shell, and nobody is going to depend on that
  284. behavior. tctpgrp returns 0 on success, -1 with errno ENOTTY if fdtty is
  285. not a terminal, -1 with errno ESRCH if pid does not exist, -1 with errno
  286. EPERM if pid exists but is not a child/descendant, -1 with errno EBADF
  287. if fdtty is not open for reading.
  288.  
  289. To implement tcnewpgrp() you need to set up a table (I recommend a
  290. chained hash table) of structures containing process group number and
  291. reference count. The reference count is the total number of processes
  292. and ttys with that process group. tcnewpgrp() can then search for a
  293. process group not in the table. The range of process group numbers is
  294. not important; a good choice for BSD systems is 32801-65000. However, it
  295. is important that there be more process groups available than the maximum
  296. possible number of ttys and pids in use at once.
  297.  
  298. Whenever a process is created, the reference count for its process group
  299. (if that group is not 0) must be incremented; whenever a process dies,
  300. the reference count for its process group (if that group is not 0) must
  301. be decremented; whenever a process changes process groups (e.g., via
  302. settpgrp()), the reference counts for old and new groups must be set
  303. appropriately; and whenever a tty changes process groups (e.g., via
  304. tcnewpgrp() or tctpgrp()), the reference counts must also be set
  305. appropriately. That's it.
  306.  
  307. A different implementation strategy has been suggested by John Carr: the
  308. system can simply assign group numbers in increasing order starting from
  309. boot time. If, for instance, a process group has 64 bits, and there are
  310. at most a billion process group manipulations per second, it will be
  311. more than 584 years before the numbers can repeat. Naturally, system
  312. administrators should keep a close eye on recently allocated process
  313. groups, and be prepared to bring the system down for maintenance as soon
  314. as there is any risk of repetition.
  315.  
  316. These three process group manipulation calls do not allow any abuse. To
  317. set a terminal to someone else's (nonzero) process group with tctpgrp(),
  318. an attacker would need a child process already in the group. But to put
  319. a process into someone else's (nonzero) process group with settpgrp(),
  320. an attacker would already need access to a tty with that group! There's
  321. no way to break into this circle. tcnewpgrp() is useless for attacks
  322. since it does not let an attacker join an existing group. Hence the
  323. system is secure. Together with the basic job control features outlined
  324. in sections 2 through 6, this provides a complete, usable job control
  325. system.
  326.  
  327. For comparison, BSD job control involves controlling ttys, and has six
  328. interface functions beyond the mechanisms mentioned in sections 2
  329. through 6: open() (of a tty), setpgrp(), getpgrp(), the TIOCGPGRP ioctl,
  330. the TIOCSPGRP ioctl, and the TIOCNOTTY ioctl. Controlling terminals
  331. affect the entire job control system and make everything harder to
  332. program and use.
  333.  
  334. POSIX job control is even worse: it includes not only the entire
  335. complexity of the BSD interface, but it has ``sessions'' with effects
  336. even more pervasive than those of controlling terminals. (For instance,
  337. a process can only be stopped if its parent is in the *same* session but
  338. a *different* process group.)
  339.  
  340.  
  341. 8. Implementing the new job control interface in a BSD system
  342.  
  343. tcnewpgrp() requires kernel changes on any system; current systems do
  344. not recognize a range of process groups to be dynamically allocated to
  345. ttys. It also allows a style of job-control programming somewhat
  346. different from the usual BSD style. However, settpgrp() and tctpgrp()
  347. can be implemented as library routines under BSD. Here they are:
  348.  
  349.    int settpgrp(fdtty)
  350.    int fdtty;
  351.    {
  352.     int pgrp = 0;
  353.     if (fdtty != -1)
  354.       if (ioctl(fdtty,TIOCGPGRP,&pgrp) == -1)
  355.         return -1;
  356.     return setpgrp(0,pgrp);
  357.    }
  358.  
  359.    int tctpgrp(fdtty,pid)
  360.    int fdtty;
  361.    int pid;
  362.    {
  363.     int pgrp;
  364.     if ((pgrp = getpgrp(pid)) == -1)
  365.       return -1;
  366.     return ioctl(fdtty,TIOCSPGRP,&pgrp);
  367.    }
  368.  
  369. Note that this interface doesn't interact with controlling ttys in any
  370. way. Unfortunately, controlling ttys sometimes force their own
  371. interactions, and a job control application which manipulates ttys (as
  372. opposed to a shell, which merely runs under a single tty) should still
  373. be aware of the old controlling tty rules. The same is true in far
  374. greater measure under POSIX---you simply cannot ignore sessions, because
  375. you will open up rather large security holes if you leave all processes
  376. in the same session. Put simply, the POSIX standard forces system code
  377. to manipulate sessions for its health.
  378.  
  379.  
  380. 9. Programming common operations with the new job control interface
  381.  
  382. Forking a pipeline in a job-control shell: The shell starts with
  383. tcnewpgrp(fdtty), so that the tty is in the new process group before
  384. there are even any children. (That's the basic difference between the
  385. BSD and POSIX models and this one.) It then forks each process in the
  386. pipeline. Each process does settpgrp(fdtty), thus joining the new
  387. process group, before it exec()s the appropriate program. Note that to
  388. avoid races the shell should block SIGCHLD while it's spawning children.
  389.  
  390. Handling a stopped child process: When the shell sees that a pipeline
  391. has stopped or exited, it does tctpgrp(fdtty,getpid()) to set the tty to
  392. its own process group. Note that it has to ignore SIGTTOU during this
  393. operation. To resume the pipeline it does tctpgrp(fdtty,pid) where pid
  394. is any one of the child processes, then sends SIGCONT to the process
  395. group.
  396.  
  397. Starting a process under a new tty: When, for instance, telnetd or
  398. init/getty or another program in process group 0 wants to grab a tty, it
  399. opens the tty and forks a child process. The child does tcnewpgrp(fdtty)
  400. to give the tty a real process group, then settpgrp(fdtty) to place
  401. itself into the foreground.
  402.  
  403. Changing ttys: Despite what POSIX would have you believe with its
  404. session straitjacket rules, people do run programs all the time under a
  405. different tty from the shell. The most common example in BSD is probably
  406. the script program; other examples are emacs, screen, pty, mtty, atty.
  407. Fortunately, exactly the same procedure works as in the previous
  408. example.
  409.  
  410. Dissociating a daemon: Note that dissociating from a tty is a
  411. controlling-terminal concept. However, most daemons do want to place
  412. themselves into process groups of their own, so that they are not
  413. affected by job-control signals. This can be handled in several ways,
  414. but by far the easiest is settpgrp(-1) to join process group 0. (Note
  415. that under BSD there is no reliable way to dissociate from a controlling
  416. tty---the TIOCEXCL ioctl can prevent dissociation. That is not the mark
  417. of a clean interface.)
  418.  
  419. Forcing oneself into the foreground: Most programs which manipulate the
  420. tty, usually so that they can run in character mode, don't work
  421. correctly with job control. The usual sequence after startup is this:
  422. read tty modes; write new tty modes including noecho and cbreak. The
  423. problem is that the process could be in the background when it reads the
  424. tty modes---a different program, which itself changes the tty modes to
  425. something strange, could be in the foreground. This process will read
  426. the strange modes, then stop when it tries to set the modes. Later it is
  427. restarted and runs without trouble---but when it exits, it will
  428. ``restore'' the tty to those strange modes it started with. To avoid
  429. this bug, processes which manipulate the tty should force themselves
  430. into the foreground before reading or writing anything. An easy way to
  431. do this is tctpgrp(getpid()), with the default SIGTTOU handler. Note
  432. that the program should also do this upon continuing after a stop---
  433. otherwise it might make the same mistake of reading modes before it
  434. knows it's in the foreground.
  435.  
  436.  
  437. 10. Helpful extensions to the job control system
  438.  
  439. There are several steps you can take which don't extend the job control
  440. interface but which do make job control programs more portable or easier
  441. to read.
  442.  
  443. As noted above, BSD has a wait3() call instead of wait2(). It is called
  444. as follows:
  445.  
  446.   #include <sys/wait.h>
  447.   #include <sys/time.h>
  448.   #include <sys/resource.h>
  449.  
  450.   int wait3(status,options,rusage)
  451.   int *status;
  452.   int options;
  453.   struct rusage *rusage;
  454.  
  455. If rusage is NULL, this is just like wait2(). <sys/time.h> can simply
  456. #include <time.h>. (Under BSD it defines several system time structures,
  457. like struct timeval.) <sys/resource.h> doesn't need to provide any
  458. information other than a definition of struct rusage. (Under BSD, if a
  459. child exits and the parent provides a non-NULL rusage pointer to
  460. wait3(), the structure is filled in with information about the resources
  461. used by the child [and its children, and so on]. For instance,
  462. ru_nsignals is the number of signals received. This is very open-ended
  463. and absolutely irrelevant to job control.) If you are adding job control
  464. to a system without it and want to provide the wait3() call, just define
  465. struct rusage { int dummy; }. While a job-control shell can make good
  466. use of resource information, most uses of wait3() really don't need the
  467. third argument. However, there are enough programs which include
  468. <sys/time.h> and <sys/resource.h> and use wait3() that it is worthwhile
  469. to provide the extra interface.
  470.  
  471. Another extension is to define a ``union wait'' type in <sys/wait.h>
  472. with an ``int w_status'' member. At some point BSD left the beaten path
  473. and decided that wait() should use a union wait instead of an int to
  474. return status information. This decision is generally regarded as a
  475. mistake if only because it severely hampers portability, but there are
  476. quite a few programs which depend on it, and there's no harm in
  477. supporting it.
  478.  
  479. More useful is to define a set of macros which extract information from
  480. a wait status. (Under BSD, union wait actually contains structure
  481. members which encode the same information. However, the macros are
  482. easier to use and support.) Here are the important ones:
  483.  
  484.    #define WIFSTOPPED(s) (((s) & 0177) == 0177)
  485.    #define WIFEXITED(s) (!((s) & 0177))
  486.    #define WIFSIGNALED(s) (0176 > (unsigned) (((s) & 0177) - 1))
  487.    #define WSTOPSIG(s) ((s) >> 8) /* only defined if WIFSTOPPED */
  488.    #define WEXITSTATUS(s) ((s) >> 8) /* only defined if WIFEXITED */
  489.    #define WTERMSIG(s) ((s) & 0177) /* only defined if WIFSIGNALED */
  490.    #define WCOREDUMP(s) ((s) & 0200) /* only defined if WIFSIGNALED */
  491.  
  492. On some 16-bit machines the >> 8 may have to be changed, or (s) may have
  493. to be cast to unsigned. These macros are meant to be applied to an int,
  494. not a union wait; most compilers will do the right thing anyway, but be
  495. careful.
  496.  
  497.  
  498. Acknowledgments
  499.  
  500. Thanks to Chris Torek for his comments. Thanks also to John F. Haugh for
  501. a series of questions which pointed out that, somewhere in this paper, I
  502. should emphasize that an ``unused'' process group is one which doesn't
  503. appear in any t_pgrp or p_pgrp. (There, I said it.)
  504.  
  505.  
  506. References
  507.  
  508. [1] D. J. Bernstein, ``A new, secure, extremely simple job control
  509. interface,'' article <18072.Jul1707.06.4191@kramden.acf.nyu.edu>,
  510. comp.unix.wizards, July 1991.
  511.